import request from '@/utils/request'
import type { RoleInfo, RoleQuery, RolePermission, RoleUser } from '@/types/role.type'

// 获取角色列表
export function getRoleList(params: RoleQuery) {
  return request({
    url: '/api/system/roles/page',
    method: 'get',
    params
  })
}

// 创建角色
export function createRole(data: Omit<RoleInfo, 'id'>) {
  return request({
    url: '/api/system/roles',
    method: 'post',
    data
  })
}

// 更新角色
export function updateRole(data: RoleInfo) {
  return request({
    url: '/api/system/roles',
    method: 'put',
    data
  })
}

// 删除角色
export function deleteRole(id: number) {
  return request({
    url: `/api/system/roles/${id}`,
    method: 'delete'
  })
}

// 获取角色权限
export function getRolePermissions(roleId: number): Promise<number[]> {
  return request({
    url: `/api/system/roles/permissions/${roleId}`,
    method: 'get'
  })
}

// 更新角色权限
export function updateRolePermissions(roleId: number, permissionIds: number[]) {
  return request({
    url: `/api/system/roles/permissions/${roleId}`,
    method: 'post',
    data: { permissionIds }
  })
}

// 获取角色用户
export const getRoleUsers = (roleId: number) => {
  return request.get(`/api/system/roles/users/${roleId}`)
}

// 更新角色用户
export function updateRoleUsers(roleId: number, userIds: number[]) {
  return request({
    url: '/api/system/roles/assign-users',
    method: 'post',
    data: {
      roleId,
      userIds
    }
  })
} 